#!/usr/bin/env python
from PyQt4 import QtCore, QtGui, QtNetwork

class PushClock(QtGui.QPushButton):
  def __init__(self, name, parent=None):
    super(PushClock, self).__init__(parent)
    self.setText("Start")
    self.clicked.connect(self.toggle)
    self.timer = QtCore.QTimer()
    self.timer.setInterval(100)
    self.timer.timeout.connect(self.updateText)
    self.savedTime = 0
    self.time=None
    self.name = name
  def toggle(self):
    if not self.isEnabled():
      self.enable()
    else:
      self.disable()
  def enable(self):
    self.time = QtCore.QTime.currentTime()
    self.setEnabled(True)
    self.updateText()
    self.timer.start()
    self.emit(QtCore.SIGNAL("enabled()"))
  def disable(self):  
    self.timer.stop()
#    self.updateText()
    
    if not self.time==None:
      i = self.time.msecsTo(QtCore.QTime.currentTime())
      self.savedTime = self.savedTime + i
    
    self.setEnabled(False)
    self.emit(QtCore.SIGNAL("disabled()"))
  def updateText(self):
    tt = QtCore.QTime()
    i = self.time.msecsTo(QtCore.QTime.currentTime())
    i = i + self.savedTime
    tt = tt.addMSecs(i)
    self.setText(tt.toString())

class ChessClock (QtGui.QWidget):
  def __init__(self, parent=None):
    super(ChessClock, self).__init__(parent)
    self.c1 = PushClock("c1")
    self.c2 = PushClock("c2")
    
    self.setLayout(QtGui.QHBoxLayout())
    self.layout().addWidget(self.c1)
    self.layout().addWidget(self.c2)
    
    QtCore.QObject.connect(self.c1, QtCore.SIGNAL("disabled()"), self.c2.enable)
    QtCore.QObject.connect(self.c2, QtCore.SIGNAL("disabled()"), self.c1.enable)
#    self.c1.disabled.connect(self.c2.enable)
#    self.c2.disabled.connect(self.c1.enable)

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    w = ChessClock()
    w.show()
    sys.exit(app.exec_())
